package com.example.widget

import android.content.Context
import android.os.SystemClock
import android.util.AttributeSet
import android.util.Log
import android.widget.Chronometer
import androidx.lifecycle.*

class MyChronometer @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : Chronometer(context, attrs), LifecycleObserver {

    var elpase: Long = 0

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun oncreate() {
        Log.d("MyChronometer", "ON_CREATE")

    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun startMeter() {
        Log.d("MyChronometer", "ON_RESUME")
        base = SystemClock.elapsedRealtime() - elpase
        start()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun ONSTART() {
        Log.d("MyChronometer", "ON_START")

    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun stopMeter() {
        Log.d("MyChronometer", "ON_PAUSE")

        elpase = SystemClock.elapsedRealtime() - base
        stop()
    }



    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun ON_DESTROY() {
        Log.d("MyChronometer", "ON_DESTROY")
    }


}


//class MyService : LifecycleService() {
//    private val myLifecycleObserver: MyLifecycleObserver = MyLifecycleObserver()
//
//    init {
//        lifecycle.addObserver(myLifecycleObserver)
//    }
//}
//
//
//class MyLifecycleObserver : LifecycleObserver {
//    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
//    private fun startGetLocation() {
//        Log.i("MyLocationListener", "OnResume")
//    }
//
//    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
//    private fun stopGetLocation() {
//        Log.i("MyLocationListener", "OnPause")
//    }
//}

